<?php

namespace Admin\Controllers;

class MaterialController extends AuthController {

    public function materialGroupAction() {
        $materialGroupModel = model('Scenario/MaterialGroup');
        $_REQUEST['userId'] = 1;
        if ($_REQUEST['userId']) {
            $condition['createUser'] = $_REQUEST['userId'];
        }
        $condition['isDel'] = IS_DEL_NO;
        $count = $materialGroupModel->getCountNumber($condition);
        $list = $materialGroupModel->getLists($condition, $fieldArr = array(), $materialGroupModel->getPk() . " desc", $page = $_REQUEST['p'] ? $_REQUEST['p'] : 1, $pagesize = 10, $groupStr = "");
        //分页类
        $Page = new \Platform\Libs\Page($count, PAGE_SIZE);
        //分页跳转的时候保证查询条件
        foreach ($_REQUEST as $key => $val) {
            if ($key != "_url" && $val) {
                $Page->parameter .= "$key=" . urlencode($val) . "&";
            }
        }
        $show = $Page->show();
        $this->view->show = $show;
        $this->view->list = $list;
    }

    public function materialGroupAddAction() {
        
    }

    public function ajaxMaterialGroupAddAction() {
        $title = trim($_POST['title']) ? trim($_POST['title']) : E("请输入素材组名");
        $type = intval($_POST['type']) ? intval($_POST['type']) : E("请选择类型");
        $materialGroupModel = model('Scenario/MaterialGroup');
        $materialGroupModel->setTitle($title);
        $materialGroupModel->setType($type);
        $materialGroupModel->setCreateTime(time());
        $materialGroupModel->setCreateUser(1);
        $materialGroupModel->setUpdateTime(0);
        $materialGroupModel->setUpdateUser(0);
        $materialGroupModel->setStatus(0);
        $materialGroupModel->setIsDel(IS_DEL_NO);
        $result = $materialGroupModel->save();
        if ($result == false) {
            $messages = $materialGroupModel->getMessages();
            foreach ($messages as $message) {
                 E($message);
            }
        }
        $this->ajaxReturn();
    }

    public function materialGroupEditAction() {
        $id = intval($_REQUEST['id']) ? intval($_REQUEST['id']) : E("id不能为空");
        $materialGroupModel = model('Scenario/MaterialGroup');
        $detail = $materialGroupModel->getDataById($id);
        $this->view->detail = $detail;
    }

    public function ajaxMaterialGroupEditAction() {
        $id = intval($_REQUEST['id']) ? intval($_REQUEST['id']) : E("id不能为空");
        $title = trim($_POST['title']) ? trim($_POST['title']) : E("请输入素材组名");
        $type = intval($_POST['type']) ? intval($_POST['type']) : E("请选择类型");
        $materialGroupModel = model('Scenario/MaterialGroup');
        $materialGroup = $materialGroupModel->getDataById($id, "", true);
        if ($materialGroup == false) {
            E("素材组不存在");
        }
        $materialGroup->setTitle($title);
        $materialGroup->setType($type);
        $materialGroup->setUpdateTime(time());
        $materialGroup->setUpdateUser(IS_DEL_YES);
        $result = $materialGroup->save();
        if ($result == false) {
            $messages = $materialGroup->getMessages();
            foreach ($messages as $message) {
                E($message);
            }
        }
        $this->ajaxReturn();
    }

    public function ajaxMaterialGroupDelAction() {
        $id = intval($_REQUEST['id']) ? intval($_REQUEST['id']) : E("id不能为空");
        $materialGroupModel = model('Scenario/MaterialGroup');
        $materialGroup = $materialGroupModel->getDataById($id, "", true);
        if ($materialGroup == false) {
            E("素材组不存在");
        }
        $materialModel = model('Scenario/Material');
        $condition['isDel'] = IS_DEL_NO;
        $condition['groupId'] = $id;
        $count = $materialModel->getCountNumber($condition);
        if ($count > 0) {
            E("该素材库里面还有素材");
        }
        $materialGroup->setUpdateTime(time());
        $materialGroup->setUpdateUser(1);
        $materialGroup->setIsDel(1);
        $result = $materialGroup->save();
        if ($result == false) {
            $messages = $materialGroup->getMessages();
            foreach ($messages as $message) {
                E($message);
            }
        }
        $this->ajaxReturn();
    }

    public function materialAction() {
        $materialModel = model('Scenario/Material');
        if ($_REQUEST['id']) {
            $condition['groupId'] = $_REQUEST['id'];
        }
        $condition['isDel'] = IS_DEL_NO;
        $count = $materialModel->getCountNumber($condition);
        $list = $materialModel->getLists($condition, $fieldArr = array(), $materialModel->getPk() . " desc", $page = $_REQUEST['p'] ? $_REQUEST['p'] : 1, $pagesize = 10, $groupStr = "");
        //分页类
        $Page = new \Platform\Libs\Page($count, PAGE_SIZE);
        //分页跳转的时候保证查询条件
        foreach ($_REQUEST as $key => $val) {
            if ($key != "_url" && $val) {
                $Page->parameter .= "$key=" . urlencode($val) . "&";
            }
        }
        $show = $Page->show();
        $this->view->show = $show;
        $this->view->list = $list;
    }

    public function materialAddAction() {
        
    }

    public function ajaxMaterialAddAction() {
        $title = trim($_POST['title']) ? trim($_POST['title']) : E("请输入素材名");
        $url = trim($_POST['url']) ? trim($_POST['url']) : E("请上传素材");
        $groupId = intval($_POST['groupId']) ? intval($_POST['groupId']) : E("请选择素材分组");
        $materialGroupModel = model('Scenario/MaterialGroup');
        $materialGroup = $materialGroupModel->getDataById($groupId);
        if ($materialGroup == false) {
            E("素材组不存在");
        }
        $materialModel = model('Scenario/Material');
        $materialModel->setName($title);
        $materialModel->setType($materialGroup['type']);
        $materialModel->setUrl($url);
        $materialModel->setGroupId($groupId);
        if ($materialGroup['type'] == 2) {
            $videoInfo = $this->getMp4Info($url);
            $materialModel->setDuration(intval($videoInfo['Duration']));
            $materialModel->setVideoCover($videoInfo['coverURL']);
        }
        $materialModel->setCreateTime(time());
        $materialModel->setCreateUser(1);
        $materialModel->setStatus(0);
        $materialModel->setIsDel(IS_DEL_NO);
        $result = $materialModel->save();
        if ($result == false) {
            $messages = $materialModel->getMessages();
            foreach ($messages as $message) {
                 E($message);
            }
        }
        $this->ajaxReturn();
    }

    public function materialEditAction() {
        $id = intval($_REQUEST['id']) ? intval($_REQUEST['id']) : E("id不能为空");
        $materialModel = model('Scenario/Material');
        $detail = $materialModel->getDataById($id);
        $this->view->detail = $detail;
    }

    public function ajaxMaterialEditAction() {
        $title = trim($_POST['title']) ? trim($_POST['title']) : E("请输入素材名");
        $url = trim($_POST['url']) ? trim($_POST['url']) : E("请上传素材");
        $id = intval($_POST['id']) ? intval($_POST['id']) : E("请选择素材");
        $materialModel = model('Scenario/Material');
        $material = $materialModel->getDataById($id, "", true);
        if ($material == false) {
            E("素材不存在");
        }
        $material->setName($title);
        $material->setUrl($url);
        $result = $material->save();
        if ($result == false) {
            $messages = $material->getMessages();
            foreach ($messages as $message) {
                E($message);
            }
        }
        $this->ajaxReturn();
    }

    public function ajaxMaterialDelAction() {
        $id = intval($_REQUEST['id']) ? intval($_REQUEST['id']) : E("id不能为空");
        $materialModel = model('Scenario/Material');
        $material = $materialModel->getDataById($id, "", true);
        if ($material == false) {
            E("素材组不存在");
        }
        $material->setIsDel(1);
        $result = $material->save();
        if ($result == false) {
            $messages = $material->getMessages();
            foreach ($messages as $message) {
                E($message);
            }
        }
        $this->ajaxReturn();
    }

    /**
     * 获取视频地址封面
     * @param type $videoUrl
     */
    public function getMp4Info($videoUrl) {
        if (strstr($videoUrl, OSS_RED_PACKET)) {
            $videoUrl = str_replace(OSS_RED_PACKET, "http://redpacket-video.oss-cn-hangzhou.aliyuncs.com", $videoUrl);
        }
        $videoInfo = lib('AliMTS')->getFileInfo($videoUrl);
        if (strstr($videoInfo['coverURL'], "http://redpacket-video-acode.oss-cn-hangzhou.aliyuncs.com")) {
            $videoInfo['coverURL'] = str_replace("http://redpacket-video-acode.oss-cn-hangzhou.aliyuncs.com", OSS_RED_PACKET_COVER, $videoInfo['coverURL']);
        } else {
            $videoInfo['coverURL'] = "https://ossredpacketimg.gjgj.com/pic_moren.png";
        }
        return $videoInfo;
    }

}
